package com.hunttown.mes.manage.service;

import com.hunttown.mes.common.keys.KeyConstants;
import com.hunttown.mes.common.cache.Cache;
import com.hunttown.common.domain.Page;
import com.hunttown.mes.common.utils.FormatUtils;
import com.hunttown.mes.rpc.api.TransportApiApplyRpcService;
import com.hunttown.mes.rpc.domain.TransportApiApplyDTO;
import com.hunttown.mes.rpc.domain.query.TransportApiApplyDTOQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * created by wangjunfu on 2019-07-04 19:01:06
 */
@Service
public class TransportApiApplyManageService {

    private final static Logger logger = LoggerFactory.getLogger(TransportApiApplyManageService.class);

    private final TransportApiApplyRpcService rpcService;
    private final Cache jedisCache;

    @Autowired
    public TransportApiApplyManageService(TransportApiApplyRpcService rpcService, Cache jedisCache) {
        this.rpcService = rpcService;
        this.jedisCache = jedisCache;
    }

    // 新增
    public TransportApiApplyDTO insert(TransportApiApplyDTO objDTO) {
        return rpcService.insert(objDTO);
    }

    // 通过ID修改
    public Boolean updateInfoById(TransportApiApplyDTO objDTO) {
        Boolean isNext = rpcService.updateInfoById(objDTO);
        if (isNext) {
            // 刷新缓存
            refreshCacheById(objDTO.getId());
        }
        return isNext;
    }

    // 通过Query修改
    public Boolean updateInfoByQuery(TransportApiApplyDTOQuery objDTOQuery) {
        Boolean isNext = rpcService.updateInfoByQuery(objDTOQuery);

        //单体刷新
        if (isNext && objDTOQuery.getW_id() != null) {
            refreshCacheById(objDTOQuery.getW_id());
        }

        //批量刷新缓存
        if (isNext && objDTOQuery.getIds() != null) {
            List<Integer> list = new ArrayList<>();
            try {
                list = (List<Integer>) objDTOQuery.getIds();
            } catch (Exception e) {
                e.printStackTrace();
            }
            refreshCacheByIdList(list);
        }
        return isNext;
    }

    // 通过ID删除
    public Boolean delete(Integer id) {
        Boolean isNext = rpcService.deleteById(id);
        if (isNext) {
            // 刷新缓存
            refreshCacheById(id);
        }
        return isNext;
    }

    // 批量删除
    public Boolean deleteForBatch(String clearIds) {

        //处理 “,5,6,7,” 为List
        List<Integer> idList = FormatUtils.commaToList_Integer(clearIds);

        if (idList.size() == 0) {
            return false;
        }

        TransportApiApplyDTOQuery query = new TransportApiApplyDTOQuery();
        query.setDeleteFlag(1);
        query.setIds(idList);

        return rpcService.updateInfoByQuery(query);
    }

    // 通过ID获取
    public TransportApiApplyDTO getById(Integer id) {
        if (id <= 0) {
            return null;
        }
        return rpcService.selectById(id);
    }

    // 通过Query获取
    public TransportApiApplyDTO getByQuery(TransportApiApplyDTOQuery objDTOQuery) {
        return rpcService.selectObjByQuery(objDTOQuery);
    }

    // 获取分页列表
    public Page<TransportApiApplyDTO> getForPage(TransportApiApplyDTOQuery objDTOQuery) {
        return rpcService.getListForPage(objDTOQuery);
    }

    // 改变某条记录的使用状态
    public Boolean changeState(Integer id, Integer state) {
        TransportApiApplyDTO obj = new TransportApiApplyDTO();
        obj.setStopFlag(state);
        obj.setId(id);
        return updateInfoById(obj);
    }

    //region 刷新缓存

    /**
     * 刷分类缓存
     *
     * @param id id
     */
    public void refreshCacheById(Integer id) {
        if (id == null || id <= 0) {
            return;
        }

        TransportApiApplyDTO obj = getById(id);
        if (obj != null) {
            jedisCache.delete(KeyConstants.API_INFO_TOKEN_KEY + obj.getToken());
        }
    }

    /**
     * 刷分类缓存(批量)
     *
     * @param idList
     */
    public void refreshCacheByIdList(List<Integer> idList) {
        if (idList == null || idList.size() == 0) {
            return;
        }

        for (Integer id : idList) {
            refreshCacheById(id);
        }
    }
    //endregion

}